package com.ruoyi.system.service.impl;


import com.ruoyi.system.domain.MealStatisticDTO;
import com.ruoyi.system.mapper.MealStatisticMapper;
import com.ruoyi.system.service.IMealStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MealStatisticServiceImpl implements IMealStatisticService {

    @Autowired
    private MealStatisticMapper mealStatisticMapper;

    @Override
    public List<MealStatisticDTO> getMealStatistic(String day) {
        return mealStatisticMapper.selectMealStatisticByDay(day);
    }
}



//package com.ruoyi.system.service.impl;
//
//
//import com.ruoyi.system.domain.MealStatisticDTO;
//import com.ruoyi.system.mapper.MealStatisticMapper;
//import com.ruoyi.system.service.IMealStatisticService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//@Service
//public class MealStatisticServiceImpl implements IMealStatisticService {
//
//    private final MealStatisticMapper mealStatisticMapper;
//
//    @Autowired
//    public MealStatisticServiceImpl(MealStatisticMapper mealStatisticMapper) {
//        this.mealStatisticMapper = mealStatisticMapper;
//    }
//
//    @Override
//    public List<MealStatisticDTO> getMealStatisticsByDate(String date) {
//        // 验证日期格式
//        if (!isValidDate(date)) {
//            throw new IllegalArgumentException("日期格式不正确，应为yyyy-MM-dd格式");
//        }
//
//        return mealStatisticMapper.getMealStatisticsByDate(date);
//    }
//
//    /**
//     * 验证日期格式是否为yyyy-MM-dd
//     * @param dateStr 日期字符串
//     * @return 是否有效
//     */
//    private boolean isValidDate(String dateStr) {
//        if (!StringUtils.hasText(dateStr)) {
//            return false;
//        }
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        sdf.setLenient(false); // 严格模式
//
//        try {
//            // 尝试解析日期
//            Date date = sdf.parse(dateStr);
//            // 验证格式是否正确
//            return dateStr.equals(sdf.format(date));
//        } catch (ParseException e) {
//            return false;
//        }
//    }
//}